home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / graph_layout / window.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  4.7 KB  |  168 lines

  1. /***************************************************************************
  2. **    TEST FILE FOR graph (Dynamic Layout Alg)
  3. **
  4. **    MODUL: C++ X-WINDOW class library based on Xlib
  5. **
  6. ** Author: dr. Szirmay-Kalos Laszlo (szirmay@fsz.bme.hu)
  7. **       Technical University of Budapest, Hungary
  8. ****************************************************************************/
  9. #include "window.hxx"
  10.  
  11. //-------------------------------------------------------------------
  12. void App :: Error( char * message, int line )
  13. //-------------------------------------------------------------------
  14. {
  15.     fprintf( stderr, "ERROR: %s", message );
  16.     if ( line >= 0 ) fprintf( stderr, " in line %d", line );
  17.     fprintf( stderr, "\n" );
  18.     Quit( );
  19. }
  20.  
  21. //-------------------------------------------------------------------
  22. void App :: Warning( char * message )
  23. //-------------------------------------------------------------------
  24. {
  25.     fprintf( stderr, "WARNING: %s\n", message );
  26. }
  27.  
  28. //-------------------------------------------------------------------
  29. void App :: Quit( )
  30. //-------------------------------------------------------------------
  31. {
  32.     fprintf( stderr, "Bye ( Graph )\n" );
  33.     exit( -1 );
  34. }
  35.  
  36. //-------------------------------------------------------------------
  37. AppWindow ::  AppWindow( int argc, char * argv[] )
  38. //-------------------------------------------------------------------
  39.        : canvas( 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT ),
  40.          beam( 0, 0 )
  41. {
  42.     if ( !(dpy = XOpenDisplay (""))) {
  43.         fprintf (stderr, "Error:  Can't open display\n");
  44.         exit (1);
  45.     }
  46.  
  47.     screen = DefaultScreen (dpy);
  48.     win = XCreateSimpleWindow(dpy,
  49.                   DefaultRootWindow (dpy),
  50.                   0, 0,
  51.                   WINDOW_WIDTH, WINDOW_HEIGHT,
  52.                   1,
  53.                   WhitePixel (dpy, screen),
  54.                   BlackPixel (dpy, screen));
  55.  
  56.     XSelectInput (dpy, win, StructureNotifyMask |
  57.                 ExposureMask |
  58.                 KeyPressMask );
  59.  
  60.     XStoreName (dpy, win, argv[0] );
  61.     XMapWindow (dpy, win);
  62. /*
  63. *  White/Black graphics context
  64. */
  65.     gc = XCreateGC (dpy, win, 0L, (XGCValues *) 0);
  66.     XSetBackground (dpy, gc, BlackPixel (dpy, screen));
  67.     XSetForeground (dpy, gc, WhitePixel (dpy, screen));
  68. /*
  69. *  Black/White graphics context
  70. */
  71.     gc_inv = XCreateGC (dpy, win, 0L, (XGCValues *) 0);
  72.     XSetForeground (dpy, gc_inv, BlackPixel (dpy, screen));
  73.     XSetBackground (dpy, gc_inv, WhitePixel (dpy, screen));
  74. }
  75.  
  76. //-------------------------------------------------------------------
  77. void AppWindow :: MessageLoop()
  78. //-------------------------------------------------------------------
  79. {
  80.     for ( ; ; ) {
  81.         XEvent event;
  82.         XNextEvent (dpy, &event);
  83.  
  84.         switch (event.type) {
  85.         case ConfigureNotify:
  86.             canvas = RectAngle( 0, 0, event.xconfigure.width, event.xconfigure.height );
  87.             break;
  88.         case Expose:
  89.             {
  90.                 XClearWindow(dpy, win);
  91.                 ExposeEvent evt( &event.xexpose );
  92.                 ExposeAll( &evt );
  93.             }
  94.             break;
  95.         case KeyPress:
  96.             {
  97.                 KeyEvent evt( &event.xkey );
  98.                 KeyPressed( &evt );
  99.             }
  100.             break;
  101.         }
  102.       }
  103. }
  104.  
  105. //-------------------------------------------------------------------
  106. RectAngle AppWindow :: Canvas()
  107. //-------------------------------------------------------------------
  108. {
  109.     return canvas;
  110. }
  111.  
  112. //-------------------------------------------------------------------
  113. void AppWindow :: RePaint()
  114. //-------------------------------------------------------------------
  115. {
  116.     XClearWindow(dpy, win);
  117.     ExposeAll( NULL );
  118. }
  119.  
  120. //-------------------------------------------------------------------
  121. void AppWindow :: Text( char * text, Point p)
  122. //-------------------------------------------------------------------
  123. {
  124.     XDrawString( dpy, win, gc, p.X(), p.Y(), text, strlen(text) );
  125. }
  126.  
  127. //-------------------------------------------------------------------
  128. void AppWindow :: MoveTo( Point p )
  129. //-------------------------------------------------------------------
  130. {
  131.     beam = p;
  132. }
  133.  
  134. //-------------------------------------------------------------------
  135. void AppWindow :: LineTo( Point p )
  136. //-------------------------------------------------------------------
  137. {
  138.     XDrawLine( dpy, win, gc, beam.X(), beam.Y(), p.X(), p.Y() );
  139.     beam = p;
  140. }
  141.  
  142. //-------------------------------------------------------------------
  143. void AppWindow :: DrawRectangle( RectAngle& rect )
  144. //-------------------------------------------------------------------
  145. {
  146.     XFillRectangle( dpy, win, gc,
  147.             rect.HorPos(), rect.VerPos(),
  148.             rect.Width(), rect.Height() );
  149.     XFillRectangle( dpy, win, gc_inv,
  150.             rect.HorPos() + 2, rect.VerPos() + 2,
  151.             rect.Width() - 4, rect.Height() - 4 );
  152. }
  153.  
  154. /************************************************************************/
  155. /*  PROGRAM ENTRY POINT                            */
  156. /************************************************************************/
  157. App    app;    // Application program object
  158.  
  159. int main( int argc, char * argv[] )
  160. {
  161. /*
  162. *   CALL APPLICATION DEPENDENT ENTRY
  163. */
  164.     app.Start( argc, argv );
  165.     app.Quit( );
  166. }
  167.  
  168.